package com.gmu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmu.domain.ResResult;
import com.gmu.mapper.TblUserMapper;
import com.gmu.pojo.TblUser;
import com.gmu.pojo.vo.RegisterVo;
import com.gmu.service.TblUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-24
 */
@RestController
@Api(tags = "用户信息表")
@RequestMapping("/gmu/tbl-user")
public class TblUserController {

    @Resource
    private TblUserService userService;

    @Autowired
    private TblUserMapper tblUserMapper;


    //查询所有用户信息
    @GetMapping("listAll")
    @ApiOperation("所有用户信息")
    public ResResult listAll(){
        List<TblUser> userList = userService.list(null);
        return new ResResult(200,"查询所有用户信息成功",userList);
    }


    //修改用户信息
    @PostMapping("updateUser")
    @ApiOperation("修改用户")
    public ResResult save(@RequestBody RegisterVo registerVo) {
      return userService.updateUser(registerVo);
    }

    //根据id删除用户信息
    @DeleteMapping("deleteUser/{userId}")
    @ApiOperation("删除用户信息")
    public ResResult deleteUser(@PathVariable Integer userId){
        boolean removeById = userService.removeById(userId);
        if(removeById){
            return new ResResult(200,"删除成功");
        }else{
            return new ResResult(500,"删除失败");
        }
    }


    // 分页查询用户的方法
    @ApiOperation("分页查询")
    @GetMapping("pageUser/{current}/{limit}")
    public ResResult pageListUser(@PathVariable long current,
                                     @PathVariable long limit) {
        //创建page对象
        Page<TblUser> pageUser = new Page<>(current, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageTeacher对象里面
        userService.page(pageUser, null);

        long total = pageUser.getTotal();//总记录数
        List<TblUser> records = pageUser.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return new ResResult(200,map);
    }


    //修改用户信息
    @PostMapping("update")
    @ApiOperation("Pc端修改用户")
    public ResResult Pcsave(@RequestBody RegisterVo registerVo) {
        return userService.PcupdateUser(registerVo);
    }

    //注册用户信息
    @ApiOperation("Pc端添加用户")
    @PostMapping("/add")
    public ResResult add(@RequestBody RegisterVo registerVo){
        return userService.add(registerVo);
    }




}

